package com.robotserver.orders.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.robotserver.orders.service.MiniProSubMsgService;
import com.robotserver.orders.service.OrdersNotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * @Auther: jianghan
 * @Date: 2020/2/7 10:02
 * @Description:
 */
@Service
public class OrdersNotifyServiceImpl implements OrdersNotifyService {

    private static Logger logger = LoggerFactory.getLogger(OrdersNotifyServiceImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Async("taskExecutor")
    @Override
    public void tbOrderRomoteLimit(Long userId, String content){

            //String url_2V = "http://172.19.99.156:9091/minipro/submsg/tbTokenErr";
            //String url_3V = "http://172.19.104.67:9222/sys/msg/push/tb_token_err";



//            try{
//                HttpHeaders headers_2V = new HttpHeaders();
//                headers_2V.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//                HttpEntity<MultiValueMap<String, String>> request_2V = new HttpEntity<>(param, headers_2V);
//                ResponseEntity<JSONObject> response_2V = restTemplate.postForEntity(url_2V, request_2V, JSONObject.class);
//
//                logger.info("---调用2.0订单库通知接口---response={}",response_2V.getBody().toJSONString());
//
//                if (response_2V.getStatusCode() == HttpStatus.OK){
//                    logger.info("---调用2.0订单库通知淘宝订单异常成功---");
//                }else{
//                    logger.error("---调用2.0订单库通知淘宝订单异常失败---error={}",response_2V.getBody());
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//                logger.error("调用2.0订单库通知异常，eror={}",e.getMessage());
//            }
//
//            try{
//                HttpHeaders headers_3V = new HttpHeaders();
//                headers_3V.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//                HttpEntity<MultiValueMap<String, String>> request_3V = new HttpEntity<>(param, headers_3V);
//                ResponseEntity<JSONObject> response_3V = restTemplate.postForEntity(url_3V, request_3V, JSONObject.class);
//
//                logger.info("---调用3.0订单库通知接口---response={}",response_3V.getBody().toJSONString());
//
//                if (response_3V.getStatusCode() == HttpStatus.OK){
//                    logger.info("---调用3.0订单库通知淘宝订单异常成功---");
//                }else{
//                    logger.error("---调用3.0订单库通知淘宝订单异常失败---error={}",response_3V.getBody());
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//                logger.error("调用3.0订单库通知异常，eror={}",e.getMessage());
//            }


            //发送悠哉钉钉报警消息
//            try{
//                //新版增加运行钉钉消息
//                String url_runLog = "http://uz-admin.52youzai.com/api/sys/runlog/warning";
//                HashMap<String,String> param = new HashMap<>();
//                param.put("text", content);
//                //param.add("atAll", String.valueOf(true));
//                HttpHeaders headers_runLog = new HttpHeaders();
//                headers_runLog.setContentType(MediaType.APPLICATION_JSON);
//                HttpEntity<Object> request_runLog = new HttpEntity<>(param, headers_runLog);
//                ResponseEntity<JSONObject> response_runLog = restTemplate.postForEntity(url_runLog, request_runLog, JSONObject.class);
//
//                logger.info("---调用运行错误日志通知接口---response={}", response_runLog.getBody().toJSONString());
//
//                if (response_runLog.getStatusCode() == HttpStatus.OK){
//                    logger.info("---调用运行错误日志通知淘宝订单异常成功---");
//                }else{
//                    logger.error("---调用运行错误日志通知淘宝订单异常失败---error={}",response_runLog.getBody());
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//                logger.error("调用运行错误日志通知异常，eror={}",e.getMessage());
//            }

            //发送淘宝订单限流报警消息
            try{
                //新版增加运行钉钉消息
                String url_alarmMsg = "http://3.52youzai.com/api/console/sendAlarmMsg/tbOrdersDownLimit";
                HashMap<String,String> param_alarmMsg = new HashMap<>();
                param_alarmMsg.put("tbTokenUserId", String.valueOf(userId));
                HttpHeaders headers_alarmMsg = new HttpHeaders();
                headers_alarmMsg.setContentType(MediaType.APPLICATION_JSON);
                HttpEntity<Object> request_alarmMsg = new HttpEntity<>(param_alarmMsg, headers_alarmMsg);
                ResponseEntity<JSONObject> response_runLog = restTemplate.postForEntity(url_alarmMsg, request_alarmMsg, JSONObject.class);

                logger.info("---调用发送淘宝订单下载限流报警消息接口---response={}", response_runLog.getBody().toJSONString());

                if (response_runLog.getStatusCode() == HttpStatus.OK){
                    logger.info("---调用发送淘宝订单下载限流报警消息成功---");
                }else{
                    logger.error("---调用发送淘宝订单下载限流报警消息失败---error={}",response_runLog.getBody());
                }
            }catch (Exception e){
                e.printStackTrace();
                logger.error("调用发送淘宝订单下载限流报警消息异常，eror={}",e.getMessage());
            }


    }

    @Async("taskExecutor")
    @Override
    public void dingdingMsgAlarm(String content){
          //发送悠哉钉钉报警消息
            try{
                //新版增加运行钉钉消息
                String url_runLog = "http://uz-admin.52youzai.com/api/sys/runlog/warning";
                HashMap<String,String> param = new HashMap<>();
                param.put("text", content);
                param.put("atAll", String.valueOf(true));
                HttpHeaders headers_runLog = new HttpHeaders();
                headers_runLog.setContentType(MediaType.APPLICATION_JSON);
                HttpEntity<Object> request_runLog = new HttpEntity<>(param, headers_runLog);
                ResponseEntity<JSONObject> response_runLog = restTemplate.postForEntity(url_runLog, request_runLog, JSONObject.class);

                logger.info("---调用钉钉报警消息通知接口---response={}", response_runLog.getBody().toJSONString());

                if (response_runLog.getStatusCode() == HttpStatus.OK){
                    logger.info("---调用调用钉钉报警消息通知接口成功---");
                }else{
                    logger.error("---调用调用钉钉报警消息通知接口失败---error={}",response_runLog.getBody());
                }
            }catch (Exception e){
                e.printStackTrace();
                logger.error("调用调用钉钉报警消息通知接口异常，eror={}",e.getMessage());
            }
    }

}
